package com.example.cinemaorder.service.impl;

import com.example.cinemaorder.entity.SysBill;
import com.example.cinemaorder.entity.vo.SearchMovieVo;
import com.example.cinemaorder.entity.vo.SysBillVo;
import com.example.cinemaorder.mapper.SysBillMapper;
import com.example.cinemaorder.service.SysBillService;
import com.example.cinemaorder.service.SysSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.example.cinemaorder.consumer.GetOrderDeadDealConsumer.cancelTimeoutBillSessionSeats;

/**
 * @author 高垚淼
 * @version 1.0
 */
@Service
public class SysBillServiceImpl implements SysBillService {

    @Autowired
    SysBillMapper sysBillMapper;
    @Autowired
    SysSessionService sysSessionService;

    @Override
    public void insertBill(SysBill sysBill) {
        sysBillMapper.insertBill(sysBill);
    }

    @Override
    public SysBill getBillById(Long billId) {
        return sysBillMapper.getBillById(billId);
    }

    @Override
    public SearchMovieVo getSessionById(Long sessionId) {
        return sysBillMapper.getSessionById(sessionId);
    }

    @Override
    public SearchMovieVo getCinemaInfo(Long id) {
        return sysBillMapper.getCinemaInfo(id);
    }

    @Override
    public Object movieName(Long id) {
        return sysBillMapper.movieName(id);
    }

    @Override
    public SysBill bill(Long id) {
        return sysBillMapper.bill(id);
    }

    @Override
    public SysBill getBillBySeats(String seats) {
        return sysBillMapper.getBillBySeats(seats);
    }

    @Override
    public Object cancelBill(SysBillVo sysBillVo) {
        //修改订单状态
        sysBillMapper.updateCancelState(sysBillVo.getSysBill().getBillId());
        //生成取消时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String formatDate = simpleDateFormat.format(date);
        sysBillMapper.updateCancelTime(sysBillVo.getSysBill().getBillId(),formatDate);
        //还原座位信息
            // 查询出排片信息
        SearchMovieVo curSession = sysBillMapper.getSessionById(sysBillVo.getSysBill().getSessionId());
             // 获取当前超时订单座位信息
        SysBill cancelBill = sysBillVo.getSysBill();
        String[] selectSeats = cancelBill.getSeats().split(",");
            // 订单已选座位
        Map<String, Integer> selectedSeatsMap = new LinkedHashMap<>();
        for (int i = 0; i < selectSeats.length; i++) {
            String row = selectSeats[i].substring(selectSeats[i].indexOf("\"") + 1, selectSeats[i].indexOf("排"));
            Integer col = Integer.parseInt(selectSeats[i].substring(selectSeats[i].indexOf("排") + 1, selectSeats[i].indexOf("座")));
            selectedSeatsMap.put(row, col);
        }
            // 取消场次座位占座
        String newSessionSeats = cancelTimeoutBillSessionSeats(curSession.getSessionSeats(), selectedSeatsMap);
        curSession.setSessionSeats(newSessionSeats);
        sysSessionService.updateSession(curSession);
        return null;
    }

    @Override
    public void payBill(SysBill sysBill) {
        sysBillMapper.payBill(sysBill);
    }


    @Override
    public void clearInvalidOrder() {
        sysBillMapper.clearInvalidOrder();
    }
}
